package com.yomob.ml.recommand

import org.apache.spark.mllib.classification.NaiveBayes
import org.apache.spark.mllib.util.MLUtils
import org.apache.spark.{SparkConf, SparkContext}

object NaiveBayesCtrPredict {

  def main(args: Array[String]): Unit = {
    //read data
    val conf = new SparkConf().setAppName("bayes").setMaster("local")
    val sc = new SparkContext(conf)
    val data = MLUtils.loadLibSVMFile(sc,this.getClass.getClassLoader.getResource("sample_libsvm_data.txt").getFile)
    val splits = data.randomSplit(Array(0.6,0.4),seed = 11L)
    val training = splits(1)
    val test  = splits(1)
    //bernoulli or multinomial
    val model = NaiveBayes.train(training,lambda = 1.0,modelType = "multinomial")
    val predictionAndLabel = test.map(p => (model.predict(p.features),p.label))
    predictionAndLabel.foreach(println)
    val accuracy = 1.0 * predictionAndLabel.filter(x => x._1 == x._2).count()/test.count()
    println(accuracy)
  }
}
